package com.example.exercise;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Looper;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.exercise.Base.SignBase;
import com.example.exercise.MysqlUtil.ClassOpt;
import com.example.exercise.MysqlUtil.DBService;
import com.example.exercise.MysqlUtil.FindExist;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Sign2 extends AppCompatActivity {

    ProgressDialog waitingDialog;
    Button enter;
    EditText name;
    String[] classstring;
    Spinner classroom;
    TextView message;
    SignBase signBase1;
    CountDownTimer countDownTimer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign2);
        classroom = findViewById(R.id.spinner3);
        enter = findViewById(R.id.enter2);
        name = findViewById(R.id.name2);
        message = findViewById(R.id.textView63);
        message.setText("");
        classroom.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        countDownTimer = new CountDownTimer(4000,1000) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        message.setText("--------");

                    }
                });

                countDownTimer.start();
            }
        }.start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                List list= ClassOpt.getAllClass();
                classstring = new String[list.size()];
                for (int i = 0; i <list.size() ; i++) {
                    classstring[i] = (String) list.get(i);
                }
                final ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,classstring);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        classroom.setAdapter(adapter);
                    }
                });


            }
        }).start();

        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String name1 = name.getText().toString().trim();
                final String class1 = classroom.getSelectedItem().toString();
//                System.out.println(class1);
                new  Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (FindExist.getUserName(name1,class1) == 0){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    message.setText("不存在该学生，请重新输入");
                                }
                            });
                        }else {

                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    SignBase signBase = new SignBase();
                                    signBase.setTime("手动签到");
                                    signBase.setIfsign("是");
                                    List<SignBase> list1 = new ArrayList<>();
                                    list1.add(signBase);
                                    if (list1.size() == 0){
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                message.setText("读取失败，可能无信息记录");
                                            }
                                        });
                                    }else {
                                        if (DBService.update1(list1, "否", name1) != -1) {


                                            List<SignBase> list = DBService.getUserSign(name1);
                                            System.out.println("6666" + list.size());
                                            if (list.size() == 0) {
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        waitingDialog.dismiss();
                                                        message.setText("读取失败，可能无信息记录");
                                                    }
                                                });
                                            } else {

                                                signBase1 = list.get(0);
                                                final SignBase finalSignBase = signBase1;
                                                System.out.println(finalSignBase.getName() + "66666");
                                                if (finalSignBase.getTime().equals("0") || finalSignBase.getIfsign().equals("今日无记录")) {

                                                    runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            waitingDialog.dismiss();
                                                            message.setText("录入失败，请重试");
                                                        }
                                                    });
                                                } else {

                                                    runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            Toast.makeText(getApplicationContext(), "签到成功", Toast.LENGTH_SHORT).show();
                                                            message.setText("签到成功");
                                                            name.setText("");
                                                            countDownTimer.cancel();
                                                            countDownTimer.start();



                                                        }
                                                    });

                                                    System.out.println("成功");
                                                }
                                            }
                                        } else {
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Toast.makeText(getApplicationContext(), "签到失败", Toast.LENGTH_SHORT).show();
                                                    message.setText("签到失败");
                                                }
                                            });
                                        }
                                    }
                                }
                            }).start();

                        }
                    }
                }).start();

            }
        });

    }
    private void showWaitingDialog() {
        /* 等待Dialog具有屏蔽其他控件的交互能力
         * @setCancelable 为使屏幕不可点击，设置为不可取消(false)
         * 下载等事件完成后，主动调用函数关闭该Dialog
         */
        waitingDialog=
                new ProgressDialog(Sign2.this);
        waitingDialog.setTitle("正在录入");
        waitingDialog.setMessage("录入中，请务必联网");
        waitingDialog.setIndeterminate(true);
        waitingDialog.setCancelable(false);
        waitingDialog.show();
    }
}